package cn.enited.devtools.apiaggregator.utils;

import cn.hutool.core.util.NumberUtil;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * Created by wwk on 2021/1/14.
 */
public class NumUtil {

    public static Long rawValueToLong(Object rawValue, Long defaultValue) {
        if (null == rawValue) {
            return defaultValue;
        }
        if (rawValue instanceof Number) {
            BigDecimal decimal = NumberUtil.toBigDecimal((Number) rawValue);
            return decimal.longValue();
        }
        return defaultValue;
    }

    public static Long rawValueToLong(Object rawValue) {
        return rawValueToLong(rawValue, null);
    }

    public static boolean isValidId(Long possibleId) {
        return Objects.nonNull(possibleId) && possibleId.compareTo(0L) > 0;
    }

    public static boolean isValidPageNumber(Integer index) {
        return Objects.nonNull(index) && index.compareTo(0) > 0;
    }

    public static boolean isValidSize(Integer size) {
        return Objects.nonNull(size) && size.compareTo(0) > 0;
    }
}
